
#include "processbar.h"

const char *lable="|/-\\";

//version 1
void processbar(){
  char buffer[NUM];
  memset(buffer,'\0',sizeof(buffer));
  int count=0;
  int n=strlen(lable);
  buffer[0]=Head;
  while(count<=100){
    printf("[%-100s][%3d%%][%c]\r",buffer,count,lable[count%n]);
    fflush(stdout);
    buffer[count++]=Body;
    if(count<100)
      buffer[count]=Head;
    usleep(100000);
  }
  printf("\n");

}

//version 2
char buffer[NUM]={0};
void process_flush(double rate){
  static int count=0;
  int n=strlen(lable);
  if(rate<=1.0)
    buffer[0]=Head;
  printf("[%-100s][%.1f%%][%c]\r",buffer,rate,lable[count%n]);
  fflush(stdout);

  buffer[(int)rate]=Body;
  if((int)rate+1<100)
    buffer[(int)(rate+1)]=Head;
  if(rate>=100.0)
    printf("\n");

  count++;
  count%=n;
}
